/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.FgysEnterprise;
import com.aaron.platform.entity.GczxOrderInfo;
import com.aaron.platform.entity.ItscDomainInfo;
import com.aaron.platform.exceptions.DomainNotExistException;
import com.aaron.platform.exceptions.EnterpriseNotExistException;
import com.aaron.platform.service.IDomainService;
import com.aaron.platform.service.IEnterpriseMgr;
import com.aaron.platform.service.IOrderMgr;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author mac
 */
public class OrderApplyBean extends DataListBean {

    private static Logger logger = Logger.getLogger(OrderApplyBean.class);

    private IOrderMgr orderMgr;

    private IDomainService domainMgr;

    private IEnterpriseMgr enterpriseMgr;

    private int orderPro;

    private List<SelectItem> orderProList;

    /** 选择的区域 */
    private int domainId;

    /** 对应的单位 */
    private int enterpriseId;

    /** 选中查看明细的订单信息 */
    private GczxOrderInfo selectedOrderInfo;

    /** 当前操作 0:view 1:modify 2:add */
    private int currentOpertion = 0;

    public void setEnterpriseMgr(IEnterpriseMgr enterpriseMgr) {
        this.enterpriseMgr = enterpriseMgr;
    }

    public void setOrderMgr(IOrderMgr orderMgr) {
        this.orderMgr = orderMgr;
    }

    public void setDomainMgr(IDomainService domainMgr) {
        this.domainMgr = domainMgr;
    }

    public OrderApplyBean() {
        super();
        this.tableTitle = FacesUtil.getString("table_title_orderlist");

         // 删除、编辑、查看操作
        this.deletable = true;
        this.editable = true;
        this.viewable = true;
        multiSelectable = false;

        pagable = true;
        selectedOrderInfo = new GczxOrderInfo();
        pageInfo = new PageInfo<GczxOrderInfo>();
    }

    @Override
    public void queryData() {

        // 设置查询条件
        Properties properties = new Properties();

        // 设置订单编号为空
        properties.setProperty(GczxOrderInfo.ORDER_STATUS, String.valueOf(0));

        if (this.orderPro != 0) {
            properties.setProperty(GczxOrderInfo.ORDER_PRO, String.valueOf(this.orderPro));
        }
       
        pageInfo = orderMgr.findByCondition(properties, pageInfo.getStartIndex(), pageInfo.getPageSize());
        logger.info("Query order list finished.");
        data.setWrappedData(pageInfo.getItems());
    }

    @Override
    public void initColumnHeaders() {
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "project_code",
                FacesUtil.getString("table_column_projectcode"), false, "6%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "contact_code",
                FacesUtil.getString("table_column_contactcode"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "enterprise_name",
                FacesUtil.getString("table_column_entprisename"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "task_name",
                FacesUtil.getString("table_column_taskname"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_value",
                FacesUtil.getString("table_column_ordervalue"), false, "8%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_desc",
                FacesUtil.getString("table_column_orderdesc"), false, "15%"));
    }

    @Override
    public String getColumnValue() {
        String columnName = getColumnName();
        increaseColumnIndex();
        
        GczxOrderInfo obj = (GczxOrderInfo) data.getRowData();
        if (columnName.equals("project_code")) {
             return obj.getProjectCode();
        } else if (columnName.equals("contact_code")) {
            return obj.getContactCode();
        } else if (columnName.equals("enterprise_name")) {
            return obj.getEnterpriseInfo().getEnterpriseName();
        } else if (columnName.equals("task_name")) {
            return obj.getTaskName();
        } else if (columnName.equals("order_value")) {
            return String.valueOf(obj.getOrderValue());
        } else if (columnName.equals("order_desc")) {
            return obj.getOrderDesc();
        }
        else {
            return "";
        }

    }

    public String toApplyOrder() {
        resetOrderInfo();
        this.currentOpertion = 2;
        this.selectedOrderInfo.setOrderStatus(1);
        return "order_apply_detail";
    }

    private void resetOrderInfo() {
        selectedOrderInfo = new GczxOrderInfo();
        this.domainId = 0;
        this.enterpriseId = 0;
    }
    /**
     * 初始化查询条件
     */
    public void reset()
    {
        this.orderPro = 0;
        this.data = new ListDataModel();
        pageInfo = new PageInfo<GczxOrderInfo>();
    }

    /** 获取专业列表 */
    public List<SelectItem> getOrderProList() {
        if (this.orderProList == null) {
            orderProList = new ArrayList<SelectItem>();
            orderProList.add(new SelectItem(0, FacesUtil.getString("list_item_default")));
            orderProList.add(new SelectItem(1, FacesUtil.getString("list_item_jz")));
            orderProList.add(new SelectItem(2, FacesUtil.getString("list_item_cs")));
            orderProList.add(new SelectItem(3, FacesUtil.getString("list_item_fgys")));
            orderProList.add(new SelectItem(4, FacesUtil.getString("list_item_qyw")));
        }
        return orderProList;
    }

    /**
     * 查看明细
     * @return 返回页面导航
     */
    @Override
    public String showDetail()
    {
        // 设置操作类型：view
        this.currentOpertion = 0;

        this.selectedOrderInfo = (GczxOrderInfo) data.getRowData();
        this.domainId = selectedOrderInfo.getDomainInfo().getDomainId();
        this.enterpriseId = selectedOrderInfo.getEnterpriseInfo().getEnterpriseId();
        return "order_apply_detail";
    }

    /**
     * 编辑数据：修改审计信息
     * @return 返回页面导航
     */
    @Override
    public String editData()
    {
        // 设置操作类型：modify
        this.currentOpertion = 1;

        this.selectedOrderInfo = (GczxOrderInfo) data.getRowData();
        this.domainId = selectedOrderInfo.getDomainInfo().getDomainId();
        this.enterpriseId = selectedOrderInfo.getEnterpriseInfo().getEnterpriseId();
        return "order_apply_detail";
    }

    public String applyOrder() {

        // 检验输入
        if (this.selectedOrderInfo.getOrderPro() == 0) {
            FacesUtil.addErrorMessage(null, "er_pro_type_required");
            return null;
        } else if (StringUtils.isBlank(this.selectedOrderInfo.getProjectCode()) ||
                StringUtils.isEmpty(this.selectedOrderInfo.getProjectCode())) {
            FacesUtil.addErrorMessage(null, "er_project_code_required");
            return null;
        } else if (StringUtils.isBlank(this.selectedOrderInfo.getContactCode()) ||
                StringUtils.isEmpty(this.selectedOrderInfo.getContactCode())) {
            FacesUtil.addErrorMessage(null, "er_project_code_required");
            return null;
        } else if (StringUtils.isBlank(this.selectedOrderInfo.getTaskCode()) ||
                StringUtils.isEmpty(this.selectedOrderInfo.getTaskCode())) {
            FacesUtil.addErrorMessage(null, "er_task_code_required");
            return null;
        } else if (StringUtils.isBlank(this.selectedOrderInfo.getTaskName()) ||
                StringUtils.isEmpty(this.selectedOrderInfo.getTaskName())) {
            FacesUtil.addErrorMessage(null, "er_task_name_required");
            return null;
        } else if (this.enterpriseId == 0) {
            FacesUtil.addErrorMessage(null, "er_enterprise_name_required");
            return null;
        } else if (this.selectedOrderInfo.getOrderValue() == 0) {
            FacesUtil.addErrorMessage(null, "er_pay_value_less", this.selectedOrderInfo.getTaskName());
            return null;
        } else if (StringUtils.isBlank(this.selectedOrderInfo.getOrderDesc()) ||
                StringUtils.isEmpty(this.selectedOrderInfo.getOrderDesc())) {
            FacesUtil.addErrorMessage(null, "er_order_desc_required");
            return null;
        }

        ItscDomainInfo domainInfo = null;
        try {
            domainInfo = domainMgr.findById(domainId);
        } catch (DomainNotExistException ex) {
            FacesUtil.addErrorMessage(null, ex.getErrorCode(), domainId);
            return null;
        }
        selectedOrderInfo.setDomainInfo(domainInfo);

        FgysEnterprise enterpriseInfo = null;
        try {
            enterpriseInfo = enterpriseMgr.findEnterpriseById(enterpriseId);
        } catch (EnterpriseNotExistException ex) {
            FacesUtil.addErrorMessage(null, ex.getErrorCode(), enterpriseId);
        }
        selectedOrderInfo.setEnterpriseInfo(enterpriseInfo);

        // 检查是否存在重复的订单
        boolean isSameOrderExist = orderMgr.isSameOrderExist(selectedOrderInfo);
        if (isSameOrderExist) {
            FacesUtil.addErrorMessage(null, "er_same_order_exist", selectedOrderInfo.getContactCode(),
                    selectedOrderInfo.getTaskCode());
            return null;
        }

        // 草稿状态
        selectedOrderInfo.setOrderStatus(0);
        if (this.currentOpertion == 1) {
            orderMgr.mergeOrderInfo(selectedOrderInfo);
            FacesUtil.addInfoMessage(null, "info_update_order_apply_success");
        } else if (this.currentOpertion == 2) {
            orderMgr.addOrderInfo(selectedOrderInfo);
            FacesUtil.addInfoMessage(null, "info_add_order_apply_success");
        }

         //清空输入的值
        resetOrderInfo();

        queryData();
        return "order_apply_list";
    }

    public String toPriviousPage() {
        resetOrderInfo();
        return "order_apply_list";
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        OrderApplyBean.logger = logger;
    }

    public int getOrderPro() {
        return orderPro;
    }

    public void setOrderPro(int orderPro) {
        this.orderPro = orderPro;
    }

    public GczxOrderInfo getSelectedOrderInfo() {
        return selectedOrderInfo;
    }

    public int getCurrentOpertion() {
        return currentOpertion;
    }

    public int getDomainId() {
        return domainId;
    }

    public void setDomainId(int domainId) {
        this.domainId = domainId;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }
}
